//
//  ViewController.m
//  15-UICollectionViewLifeCycle
//
//  Created by dllo on 16/8/3.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"
#import "MYCollectionViewCell.h"
#define COLORAUTO [UIColor colorWithRed:(arc4random() % 256 / 255.0) green:(arc4random() % 256 / 255.0 )blue:(arc4random() % 256 / 255.0) alpha:1.0]

@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatCollectionView];
    /* 关于collectionView LifeCycle
     * 1.numberOfSectionInCollectionView
     * 2.collectionView:numberOfItemInsection
     * 注意 3~5循环执行
     * 3.prepareForReuse(从重用池取出时调用)
     * 4.collectionView:cellForItemAtIndexPath:
     * 5.collectionView:willDisPlayingCell
     * 当Cell完全出屏幕时
     * 6.collectionView:didEndDisPlayingCell
     * iOS10 以前
     * 出屏幕立即返回, 依旧进入重用池
     * iOS 10 以后
     * 出屏幕立即返回, 会执行4~5, 不会执行3, 提高了效率
     */
}
- (void)creatCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(100, 100);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:layout];
    collection.delegate = self;
    collection.dataSource = self;
    [self.view addSubview:collection];
    [collection registerClass:[MYCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
#pragma mark - collectionView LifeCycle
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSLog(@"%@----%s", self, __FUNCTION__);
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"%@ --- %s", self, __FUNCTION__);
    return 100;
}
// 重用池取出cell
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = COLORAUTO;
    NSLog(@"%@----%s", self, __FUNCTION__);
    return cell;
}
// iOS8.0以后推出, cell将要显示在屏幕上时
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@----%ld ------[%@ : %s]", cell, indexPath.row, self, __FUNCTION__);
}
// iOS8.0以后推出, cell已经离开在屏幕上时
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@----%ld-----[%@ : %s]", cell, indexPath.row, self, __FUNCTION__);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
